<?php
class MySqlDB{
private	 $db;
private	 $host;
private	 $user;
private	 $password;
private	 $char;
private	 $link;
private  static $instance;


private function __construct($config=''){
	//初始化方法属性，公共方法入口

  $this->_init($config);
  $this->_connect();
  $this->_charset();
  $this->_dbselect();
}

private function __clone(){

}
public static function getinstance($config){
  if(!isset(static::$instance)){
    static::$instance = new mysqldb($config);
  }
  return static::$instance;
}

private function _init($config){
  $this->host=isset($config['host'])?$config['host']:'localhost';
  $this->user=isset($config['user'])?$config['user']:'root';
  $this->password=isset($config['password'])?$config['password']:'root';
  $this->char=isset($config['char'])?$config['char']:'utf8';
  $this->db=isset($config['db'])?$config['db']:'blog';
}

private function _connect(){
	$link = @mysql_connect($this->host,$this->user,$this->password);
	if($link){
		$this->link=$link;
	}else{
		die("连接错误原因：".mysql_error());
	}
}

private function _charset(){
	mysql_set_charset($this->char);
}

private function _dbselect(){
	mysql_select_db($this->db);
}
//DQL
public function qurey($sql){
 $res = mysql_query($sql,$this->link);
 if($res){
  $return =array();
while ($row = mysql_fetch_assoc($res)) {
	$return[]=$row;
}
mysql_free_result($res);
return $return;
 }else{
 	die("sql语句有误：".$sql."错误原因为：".mysql_error());
 }
}
//DML
public function exec($sql){
 $res = mysql_query($sql,$this->link);
 if($res){
 	return  mysql_affected_rows();
 }else{
 	return false;
 }
}

//关闭数据资源
  function __destruct(){
	mysql_close($this->link);
}
}

?>